<template>
  <div class="app-container">
   

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['coordination:intro:add']"
        >新增</el-button>
      </el-col> -->
   
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['coordination:intro:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="introList" @selection-change="handleSelectionChange" show-overflow-tooltip="">
      <el-table-column type="index" width="65" align="center" label="序号" />
      <el-table-column label="收费站" align="center" prop="stationName" />
      <!-- <el-table-column label="收费站简介" align="center" prop="stationNote" /> -->
      <el-table-column label="收费站图片" align="center" prop="stationImg">
        <template #default="scope" >
          <image-preview style="margin-right: 10px;" :src="scope.row.stationImg" :index="index" :initial="index" :width="50" :height="50" v-for="(item,index) in getSplit(scope.row.stationImg)" :key="index"/>
        </template>
      </el-table-column>  

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="View" @click="handleUpdate(scope.row)" v-hasPermi="['coordination:intro:edit']">查看详情</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改机构简介对话框 -->
    <el-dialog :title="title" v-model="open" width="1000px" append-to-body>
      <el-form ref="introRef" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="收费站" prop="stationNote">
          <el-input v-model="form.stationName" type="text" placeholder="请输入" :disabled="true" />
        </el-form-item>  

        <el-form-item label="收费站图片" prop="stationImg">
          <image-upload v-model="form.stationImg" :disabled="true" />
        </el-form-item>
        <el-form-item label="收费站简介" prop="stationNote">
          <!-- <el-input v-model="form.stationNote" type="textarea" placeholder="请输入内容" :disabled="true" /> -->
          <Editor :modelValue="form.stationNote" v-model="form.stationNote" :minHeight="300" />
        </el-form-item>       
      
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <!-- <el-button type="primary" @click="submitForm">确 定</el-button> -->
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Intro">
import { listIntro, getIntroDept, delIntro, addIntro, updateIntro } from "@/api/coordination/intro";
import Editor from "@/components/Editor";
const { proxy } = getCurrentInstance();

const introList = ref([]);
const tbNearStationList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const checkedTbNearStation = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 30,
    stationId: null,
    stationImg: null,
    stationNote: null,
    tollAccYear: null,
    trafficAccYear: null,
    tollAccOpen: null,
    trafficAccOpen: null,
  },
  rules: {
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询机构简介列表 */
function getList() {
  loading.value = true;
  listIntro(queryParams.value).then(response => {
    introList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

function getSplit(str){
  let arr=str&&str.indexOf(',')!=-1?str.split(','):[str]
  return arr
}
// 表单重置
function reset() {
  form.value = {
    id: null,
    stationId: null,
    stationImg: null,
    stationNote: null,
    tollAccYear: null,
    trafficAccYear: null,
    tollAccOpen: null,
    trafficAccOpen: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null
  };
  tbNearStationList.value = [];
  proxy.resetForm("introRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加机构简介";
}

const setText=(val)=>{
  if (val != null && val != "") {
    var re1 = new RegExp("<.+?>|&.+?;","g"); //匹配html标签的正则表达式，"g"是搜索匹配多个符合的内容
    var msg = val.replace(re1,""); //执行替换成空字符
    msg = msg.replace(/\s/g,""); //去掉所有的空格（中文空格、英文空格都会被替换）
    msg = msg.replace(/[\r\n]/g,""); //去掉所有的换行符
    return msg.substr(0, 40); //获文本文字内容的前100个字符
  } else return ''
}
/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getIntroDept(_id).then(response => {
    
    response.data.stationNote=setText(response.data.stationNote);
    form.value = response.data;
    open.value = true;
    title.value = "查看机构简介";
  });
}




/** 导出按钮操作 */
function handleExport() {
  proxy.download('coordination/intro/export', {
    ...queryParams.value
  }, `机构简介_${new Date().getTime()}.xlsx`)
}

getList();
</script>
